﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Frogism.Core;
using Frogism.Domain;

namespace Frogism.Web.Controllers
{
    public class HomeController : FrogControllerBase
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Login()
        {
            Session["SaltString"] = Guid.NewGuid().ToString();
            return View();
        }
        public ViewResult Index()
        {
          IBookManager bm = ManagerService.GetBookManager();
          ViewData.Add("Books",bm.GetPublicReading());

          var list = new Data._2.NewsRepository().GetAll().OrderByDescending(a => a.CreatedDate);
          ViewData.Add("LatestNews", list);
          return View("Index");
        }


     
        public ActionResult DoLogin(string username, string hashedKey, string redirectUrl)
       { 
            var um = new UserManager();
            Core.User user = um.GetUser(username);
            if (user == null)
            {
                ViewData["StatusMessage"] = "User not found";
                return View("Login");
            }
            var prehash = user.Password + ":" + Session["SaltString"];
            var newHash = um.HashHexString(user.Password + ":" + Session["SaltString"]);
            //var newHash = user.Password;
            if (newHash != hashedKey)
            {
                ViewData["StatusMessage"] = "Password do not match";
                return View("Login");
            }
            FormsAuthentication.SetAuthCookie(username, true);
            if (redirectUrl != null && redirectUrl != string.Empty)
            {
                return Redirect(redirectUrl);
            }
            else
            {
                var url = Url.Action("Index", new { controller="Book"});
                //HttpContext.Response.Redirect("http://localhost" + url);
                ViewData["StatusMessage"] = "Login Successful";
                return RedirectToAction("Index", "Home", new { id = Guid.NewGuid().ToString() });
               // return View((object) Url.Action("Index","Home"));
            }
        }
        public ActionResult LogOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }
        public ViewResult About()
        {
            return View("About");
        }
        [AcceptVerbs("POST")]
        public ActionResult EditNews(int id, string Title, string Body)
        {
            var repos = new Data._2.NewsRepository();
            var news = repos.Get(id);
            if (news == null)
            {
                Session["StatusMessage"] = "News Item not found";

            }
            else
            {
                news.Title = Title;
                news.Body = Body;
                repos.Save(news);
            }
            return RedirectToAction("Index");
        }

        public ActionResult EditNews(int id)
        {
            var news = new Data._2.NewsRepository().Get(id);
            if (news == null)
            {
                Session["StatusMessage"] = "News Item not found";
                return RedirectToAction("Index");
            }
            ViewData.Model = news;
            return View();
        }
    }
}
